package com.syqc.videos.real

import android.view.View
import autodispose2.autoDispose
import com.syqc.base.BaseRealFragment
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragTalkBinding
import com.syqc.videos.TalkHelper
import com.syqc.videos.TalkStatus
import com.syqc.videos.TalkbackUtil
import com.syqc.videos.videomanager.PlayerManager
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

/**
 *@author  hfjs
 *@time   2024/12/31 11:02
 *@desc
 */
class TalkFragment(private val type: Int, private val callback: () -> Unit) :
    BaseRealFragment<FragTalkBinding>(type, FragTalkBinding::inflate) {
    //是否开始录音
    private var isRecord = true
    override fun hide() {
        if (isNotLoad()) return
        isRecord = true
        binding.ivAudio.setImageResource(TalkHelper.voiceImg[0])
        TalkbackUtil.closeTalk()
    }

    override fun initView(view: View) {
        binding.ivAudio.setImageResource(TalkHelper.voiceImg[0])
    }

    override fun initData() {
        handAudio()
    }

    override fun initEvent() {
        binding.tvCloseTalk.setOnClickListener {
            isRecord = true
            binding.ivAudio.setImageResource(TalkHelper.voiceImg[0])
            TalkbackUtil.closeTalk()
        }
    }

    private var isClose = false
    private fun handAudio() {
        TalkbackUtil.status.autoDispose(scopeProvider).subscribe {
            when (it) {
                TalkStatus.CLOSE -> {
                    binding.tvAudio.setText(R.string.toast_audio_close)
                    isRecord = true
                    callback()
                    isClose = true
                }

                TalkStatus.OPEN -> {
                    binding.tvAudio.setText(R.string.toast_audio_link)
                }

                TalkStatus.SUCCESS -> {
                    binding.tvAudio.setText(R.string.toast_audio_talking)
                }

                TalkStatus.FAILURE -> {
                    binding.tvAudio.text = TalkbackUtil.toast
                    isRecord = true
                    binding.tvAudio.postDelayed({
                        if (!isClose) callback()
                        isClose = true
                    }, 3000)
                }

                else -> {}
            }
        }
        //上传PCM数据网络失败监听
        TalkbackUtil.upload.autoDispose(scopeProvider).subscribe {
            isRecord = true
            TalkbackUtil.closeTalk()
        }
        TalkbackUtil.image.autoDispose(scopeProvider).subscribe {
            binding.ivAudio.setImageResource(it)
        }
        TalkbackUtil.toastLive.observeOn(AndroidSchedulers.mainThread()).autoDispose(scopeProvider)
            .subscribe {
                showToast(it)
            }
    }


    fun startRecord(carId: String, audioPass: String) {
        TalkbackUtil.openTalk(requireContext(), carId, audioPass)
        isClose = false
    }

    fun stopRecord() {
        isRecord = true
        TalkbackUtil.closeTalk()
    }

    override fun onDetach() {
        super.onDetach()
        TalkbackUtil.getPlayer()?.let {
            PlayerManager.clearAllNorAudio(it.getKey())
        }
        TalkbackUtil.destroy()
    }
}